package com.easemob.helpdesk.activity;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.housekeeperapp.R;
import com.net.ApiService;
import com.net.HKHttpUtils;
import com.net.HKResponseResultCallBack;

import java.math.BigDecimal;


import retrofit2.Call;

/**
 * Created by xiaolong.wei on 2018/11/26.
 */

public class OrderRewardRulesActivity extends BaseActivityV2 {




    TextView mTitle;
    RelativeLayout mBackgroundContainer;
    ImageView mBackground;
    TextView mTextLineOne;
    TextView mTextLineTwo;
    TextView mMoneyAmount;
    TextView mTag;

    ScrollView mContentContainer;
    ProgressBar mProgress;

    View ruleContainer;
    TextView firstClause;
    TextView secondClause;

    TextView thirdClause;
    TextView fourthClause;
    TextView fifthClause;
    TextView sixthClause;
    TextView seventhClause;
    TextView firstClauseNumber;
    LinearLayout firstClauseContainer;
    @Override
    protected void initFindView() {
        mTitle=findViewById(R.id.title);
        mBackgroundContainer=findViewById(R.id.background_container);
        mBackground=findViewById(R.id.background);
        mTextLineOne=findViewById(R.id.text_line_one);
        mTextLineTwo=findViewById(R.id.text_line_two);
        mMoneyAmount=findViewById(R.id.money_amount);
        mTag=findViewById(R.id.tag);
        mContentContainer=findViewById(R.id.content_container);
        mProgress=findViewById(R.id.progress);
        ruleContainer=findViewById(R.id.rule_container);
        firstClause=findViewById(R.id.first_clause);
        secondClause=findViewById(R.id.second_clause);

        thirdClause=findViewById(R.id.third_clause);
        fourthClause=findViewById(R.id.fourth_clause);
        fifthClause=findViewById(R.id.fifth_clause);
        sixthClause=findViewById(R.id.sixth_clause);
        seventhClause=findViewById(R.id.seventh_clause);
        firstClauseNumber=findViewById(R.id.first_clause_number);
        firstClauseContainer=findViewById(R.id.first_clause_container);

        secondClauseNumber=findViewById(R.id.second_clause_number);
        secondClauseContainer=findViewById(R.id.second_clause_container);
        thirdClauseNumber=findViewById(R.id.third_clause_number);
        thirdClauseContainer=findViewById(R.id.third_clause_container);

        fourthClauseNumber=findViewById(R.id.fourth_clause_number);
        fourthClauseContainer=findViewById(R.id.fourth_clause_container);
        fifthClauseNumber=findViewById(R.id.fifth_clause_number);
        fifthClauseContainer=findViewById(R.id.fifth_clause_container);
        sixthClauseNumber=findViewById(R.id.sixth_clause_number);
        sixthClauseContainer=findViewById(R.id.sixth_clause_container);
        seventhClauseNumber=findViewById(R.id.seventh_clause_number);
        seventhClauseContainer=findViewById(R.id.seventh_clause_container);

        View back=findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });
    }
    TextView secondClauseNumber;
    LinearLayout secondClauseContainer;
    TextView thirdClauseNumber;
    LinearLayout thirdClauseContainer;
    TextView fourthClauseNumber;
    LinearLayout fourthClauseContainer;
    TextView fifthClauseNumber;
    LinearLayout fifthClauseContainer;

    TextView sixthClauseNumber;
    LinearLayout sixthClauseContainer;
    TextView seventhClauseNumber;
    LinearLayout seventhClauseContainer;

    private int mScreenWidth;
    private int mScreenHeight;
    private int mBackgroundPicHeight;
    private int mMoneyAmountTop;
    private int mLineOneTextTop;
    private int mTagTop;

    private ApiService apiService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_reward_rules);
        initFindView();
        if (MainActivity.userRole == 0){
            mTitle.setText("超级管家订单奖励");
        }else {
            mTitle.setText("管家订单奖励");
        }
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getResources().getDisplayMetrics().heightPixels;


        getTextAndImageTops();

        RelativeLayout.LayoutParams bgLayoutParams = (RelativeLayout.LayoutParams) mBackground.getLayoutParams();
        bgLayoutParams.width = mScreenWidth;
        bgLayoutParams.height = mBackgroundPicHeight;
        mBackground.setLayoutParams(bgLayoutParams);
        mBackground.setImageResource(R.drawable.order_reward_rules_bg);
        //金额高度
        RelativeLayout.LayoutParams moneyAmountParams = (RelativeLayout.LayoutParams) mMoneyAmount.getLayoutParams();
        moneyAmountParams.topMargin = mMoneyAmountTop;
        mMoneyAmount.setLayoutParams(moneyAmountParams);


        //每月不封顶的标签顶部高度
        RelativeLayout.LayoutParams tagParams = (RelativeLayout.LayoutParams) mTag.getLayoutParams();
        tagParams.topMargin = mTagTop;
        mTag.setLayoutParams(tagParams);

        loadData();

    }

    private void loadData() {
        if (apiService == null) {
            apiService = HKHttpUtils.getApiService();
        }
        apiService.getHouseHoldsCount(MainActivity.ticket).enqueue(new HKResponseResultCallBack<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                if (MainActivity.userRole == 1 && TextUtils.isEmpty(MainActivity.village )) {
                    return;
                }
                String s = getResources().getString(R.string.HU_LI_ZHAN).equals(MainActivity.productKey)?"用户":"业主";
                firstClause.setText(Html.fromHtml("<strong>订单统计类型：</strong>生活需求类订单（代买、代送、快递等）算作奖励订单之中。报事、报修等物业管理问题不算作奖励订单之中。"));
                firstClauseNumber.setText("1、");
                secondClause.setText(Html.fromHtml("<strong>有效订单：</strong>订单由管家完成后，"+s+"进行服务评价，4分、5分即算为有效订单，1分-3分算为无效订单。"));
                secondClauseNumber.setText("2、");
                if (MainActivity.userRole == 0 || (MainActivity.userRole == 1 && !TextUtils.isEmpty(MainActivity.village))) {//物业管家====超级管家
                    thirdClauseNumber.setText("3、");
                    thirdClause.setText(Html.fromHtml("<strong>奖励规则：</strong>超级管家管理户数1-2500户时，每笔有效订单奖励金为5元；超级管家管理户数2501-5000户时，每笔有效订单奖励金为3元。"));
                    fourthClauseNumber.setText("4、");
                    fourthClause.setText(Html.fromHtml("<strong>订单统计时间：</strong>订单奖励统计时间按订单结束时间计算。"));
                    fifthClauseNumber.setText("5、");
                    fifthClause.setText(Html.fromHtml("<strong>奖励发放时间：</strong>每月核算，次月 8 号前发放。"));
                    sixthClauseNumber.setText("6、");
                    sixthClause.setText(Html.fromHtml("<strong>差评订单：</strong>每月无效订单超出总订单数的 10%后，经由信息分拣中心回访，确实属服务质量不合格导致的服务不满意，超出1单，每单罚款 20元。且个人每月整体好评度与当月绩效考核直接相关。"));
                    seventhClauseNumber.setText("7、");
                    seventhClause.setText(Html.fromHtml("<strong>特别提醒：</strong>凡有作弊行为，将取消该订单及全天全部奖励，并处以3倍同等奖励金额处罚。"));
                    seventhClauseContainer.setVisibility(View.VISIBLE);
                    if (integer != null && integer.intValue() > 0 && integer.intValue() <= 2500) {
                        mMoneyAmount.setText("5元");
                    } else {
                        mMoneyAmount.setText("3元");
                    }
                    //说明文字顶部高度
                    RelativeLayout.LayoutParams lineOneParams = (RelativeLayout.LayoutParams) mTextLineOne.getLayoutParams();
                    lineOneParams.topMargin = mLineOneTextTop;
                    mTextLineOne.setLayoutParams(lineOneParams);
                    mTextLineOne.setText(Html.fromHtml("每月订单超过<font 'color='#ff5a00'><big>40</big></font>笔后"));
                    mTextLineTwo.setVisibility(View.VISIBLE);

                } else if (MainActivity.userRole == 2) {//业主管家===楼栋管家
                    thirdClauseNumber.setText("3、");
                    thirdClause.setText(Html.fromHtml("<strong>订单统计时间：</strong>订单奖励统计时间按订单结束时间计算。"));
                    fourthClauseNumber.setText("4、");
                    fourthClause.setText(Html.fromHtml("<strong>奖励发放时间：</strong>每月核算，次月 8 号前发放。"));
                    fifthClauseNumber.setText("5、");
                    fifthClause.setText(Html.fromHtml("<strong>差评订单：</strong>每月无效订单超出总订单数的 10%后，经由信息分拣中心回访，确实属服务质量不合格导致的服务不满意，超出1单，每单罚款 20元。且个人每月整体好评度与当月绩效考核直接相关。"));
                    sixthClauseNumber.setText("6、");
                    sixthClause.setText(Html.fromHtml("<strong>特别提醒：</strong>凡有作弊行为，将取消该订单及全天全部奖励，并处以3倍同等奖励金额处罚。"));

                    seventhClauseContainer.setVisibility(View.GONE);
                    if (integer != null) {
                        mMoneyAmount.setText("5元");

                    }

                    RelativeLayout.LayoutParams lineOneParams = (RelativeLayout.LayoutParams) mTextLineOne.getLayoutParams();
                    lineOneParams.topMargin = mLineOneTextTop + 20;
                    mTextLineOne.setLayoutParams(lineOneParams);
                    mTextLineOne.setText("每完成1笔有效订单，即可获得奖励");
                    mTextLineTwo.setVisibility(View.GONE);
                }
                ruleContainer.setVisibility(View.VISIBLE);
                mProgress.setVisibility(View.GONE);
                mContentContainer.setVisibility(View.VISIBLE);


            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(OrderRewardRulesActivity.this, msg, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailed(Call call, Throwable t) {
                Toast.makeText(OrderRewardRulesActivity.this, "网络断开，请检查网络连接", Toast.LENGTH_LONG).show();

            }
        });
    }

/*    @OnClick(R.id.back)
    public void onClick(View view) {
        finish();
    }*/

    private void getTextAndImageTops() {

        BigDecimal h = new BigDecimal(1096);
        BigDecimal w = new BigDecimal(749);
        BigDecimal sw = new BigDecimal(mScreenWidth);
        BigDecimal result = h.multiply(sw).divide(w, 0, BigDecimal.ROUND_HALF_UP);


        mBackgroundPicHeight = result.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        //获取金额距顶部的高度
        BigDecimal amountTop = new BigDecimal(800);
        BigDecimal resultAmountTextTop = amountTop.multiply(result).divide(h, 0, BigDecimal.ROUND_HALF_UP);
        mMoneyAmountTop = resultAmountTextTop.intValue();
        //获取两行说明距顶部的高度
        BigDecimal lineOneTop = new BigDecimal(500);
        BigDecimal resultLineOneTop = lineOneTop.multiply(result).divide(h, 0, BigDecimal.ROUND_HALF_UP);
        mLineOneTextTop = resultLineOneTop.intValue();
        //获取"每月不封顶"标签的高度
        BigDecimal tagTop = new BigDecimal(960);
        BigDecimal resultTagTop = tagTop.multiply(result).divide(h, 0, BigDecimal.ROUND_HALF_UP);
        mTagTop = resultTagTop.intValue();


    }


}
